//|||||||||||||||||||||||||||||||||||||||||||||||

#include "MainMenuState.hpp"
#include "GameEntity.hpp"

//|||||||||||||||||||||||||||||||||||||||||||||||

using namespace Ogre;

//|||||||||||||||||||||||||||||||||||||||||||||||

MainMenuState::MainMenuState()
{
    m_bQuit = false;
	m_FrameEvent = Ogre::FrameEvent();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MainMenuState::enter()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering MainMenuState...");

    m_pSceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "MenuSceneMgr");
    m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

    m_pCamera = m_pSceneMgr->createCamera("MainMenuCam");
    m_pCamera->setPosition(Vector3(0, 25, -50));
    m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

    m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
        Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

    OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
//    OgreFramework::getSingletonPtr()->m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showCursor();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_BOTTOM, "NewBtn", "New Game", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_BOTTOM, "LoadBtn", "Load Game", 250);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_BOTTOM, "ExitBtn", "Quit", 250);

	OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_TOP,"title", "Stress Control!", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_TOPLEFT,"title2", "Stress Control!", 250);

		// center intro text
	Ogre::String introText = "Stress Control! is a game to teach you about what stress is and how to cope with it \n\n";
	introText.append("Stress is a very common problem. We all have stress in our lives. If you feel stressed you \n");
	introText.append("just have too much of a normal feeling. You should not aim to *cure* yourself of stress. You \n");
	introText.append("should aim to control it.\n\n");
	introText.append("Stress is often a mix of anxiety and depression. Anger, panic, sleep and drink problems are \n");
	introText.append("often involved. \n\n ");
	introText.append("Stress is not a mental illness. It will not turn into mental illness even if the stress is severe.\n\n");
	introText.append("Getting stress under control is a lot more complex that giving yourself a good shake. Tablets \n");
	introText.append("can help but, in the long run, you should learn ways to get back in control. You can learn to spot\n");
	introText.append("the danger signs of stress. This will help to revent problems in the future.\n\n");
	introText.append("There is no magic cure - don\'t be impatient. It takes time to control stress.\n\n");
	introText.append("Believe in yourself - you can do it.\n\n");
	introText.append("-- Jim White, Steps Glasgow, www.glasgowsteps.com");

	OgreFramework::getSingletonPtr()->m_pTrayMgr->createTextBox(OgreBites::TL_CENTER, "Intro", introText, 800, 500);		

    createScene();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MainMenuState::createScene()
{
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MainMenuState::exit()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Leaving MainMenuState...");

    m_pSceneMgr->destroyCamera(m_pCamera);
    if(m_pSceneMgr)
        OgreFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MainMenuState::keyPressed(const OIS::KeyEvent &keyEventRef)
{
    if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
    {
        m_bQuit = true;
        return true;
    }

    OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MainMenuState::keyReleased(const OIS::KeyEvent &keyEventRef)
{
    OgreFramework::getSingletonPtr()->keyReleased(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MainMenuState::mouseMoved(const OIS::MouseEvent &evt)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(evt)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MainMenuState::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(evt, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MainMenuState::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(evt, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MainMenuState::update(double timeSinceLastFrame)
{
    m_FrameEvent.timeSinceLastFrame = timeSinceLastFrame;
    OgreFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);

    if(m_bQuit == true)
    {
        shutdown();
        return;
    }
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MainMenuState::buttonHit(OgreBites::Button *button)
{
	if(button->getName() == "ExitBtn") {
        m_bQuit = true;
	} else if(button->getName() == "NewBtn") {
        changeAppState(findByName("GameProgressMenuState"));
	} else if(button->getName() == "LoadBtn") {
		
	}
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MainMenuState::setGameApp(GameApp* app)
{
	m_app = app;
}